<template>
  <el-dialog title="新增员工" :visible="showDialog" :close-on-click-modal="false" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules" top="8vh" @click.native="delDepartment">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input :value="form.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="gettingData" />
        <div v-if="showTree" class="tree-box">
          <el-tree v-loading="isLoading" :data="branchList" :props="{label: 'name'}" @node-click="nodeClick" />
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitData">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentsApi } from '@/api/departments'
import { NewEmployeeAPI } from '@/api/employee'
import Employees from '@/constant/employees'
import { TranslistTreeData } from '@/utils/index'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '角色名称不可为空', trigger: 'blur' },
          { min: 1, max: 4, message: '角色名称为1~4字数', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不可为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [{ required: true, message: '入职时间不可为空', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不可为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不可为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不可为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间不可为空', trigger: 'blur' }]
      },
      branchList: [],
      showTree: false,
      isLoading: false,
      hireType: Employees.hireType
    }
  },
  methods: {
    // 关闭弹层
    closeDialog() {
      // 关闭弹层
      this.$emit('update:showDialog', false)
      // 清空表单
      this.$refs.addForm.resetFields()
    },
    // 获取部门数据
    async gettingData() {
      // 加载圈显示
      this.isLoading = true
      // 弹层状态切换
      this.showTree = !this.showTree
      // 获取数据
      const { data: { depts }} = await getDepartmentsApi()
      // 调用转换树形结构组件
      this.branchList = TranslistTreeData(depts, '')
      // 加载圈隐藏
      this.isLoading = false
    },
    // 关闭数据
    delDepartment() {
      this.showTree = false
    },
    // 点击数据
    nodeClick(data) {
    // 判断有子级的话退出操作
      if (data.children.length > 0) return
      // 没子级赋值给表单
      this.form.departmentName = data.name
      // 关闭弹层
      this.showTree = false
    },
    // 点击提交
    submitData() {
      // 兜底校验
      this.$refs.addForm.validate(async vali => {
        if (!vali) return
        // 提示交数据
        const res = await NewEmployeeAPI(this.form)
        // 弹出提示
        this.$message.success(res.message)
        // console.log(res)
        // 关闭弹层
        this.closeDialog()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
